define(function (require) {
var zsApp= require('app');
require('selectCache');
zsApp.controller('scopeAuthorizationCtrl', ['$scope', '$http', '$timeout', 'selectCache', 'promptCache', function($scope, $http, $timeout, selectCache, promptCache) {

	// pagination
	$scope.paginationConf = {
        currentPage		: 1,
        totalItems		: 10,
        itemsPerPage	: 10,
        pagesLength		: 15,
        perPageOptions	: [10, 20, 30, 40, 50],
        onChange: function(){
        	var self = this,
        		pagingData = {
	        		pageNumber  : self.currentPage,  
					pageSize 	: self.itemsPerPage,  
	        	};	
	
			$http
			({
				url  	: $scope.zsUrl+'digitalhome-boss/open/scopes/getList/',
				method 	: 'POST',
				data 	: pagingData,

			}).success(function(res) {

				if(res.result_code != '0000') {
                    $scope.isNoData = false;
                    return false;
                }

				// 系统配置键列表页面表格
				$scope.tabVal = res.result;
				// 遍历每条数据对应的ID
				angular.forEach($scope.tabVal, function(obj, i) {
					$scope.dataID[i] = obj.scopesId;
				});
				// 总共有多少条数据
				self.totalItems = res.length;

			}).error(function(err) {
				console.log(err);
			});
        }
    };

    // newlyAdded: show serviceType select data
    $scope.newlyAdded = function() { 
    	getServiceTypeData('newlyAdded');
    };

    // NewlyAdded submit
    $scope.submitNewlyAdded = function() {

    	var newlyAddedData = {
    		scopesCode 		: $scope.newlyAdded_scopesCode,
    		scopeName 		: $scope.newlyAdded_scopeName,
    		scopeUri 		: $scope.newlyAdded_scopeUri,
    		scopeType 		: $scope.newlyAdded_scopeType,		 // 资源类型
    		serviceType 	: $scope.newlyAdded_serviceType,	 // 服务类型
    		serviceTypeName : $scope.newlyAdded_serviceTypeName, // 服务类型 name
    		description 	: $scope.newlyAdded_description,
    	};

    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/open/scopes/add',
    		method	: 'POST',
    		data 	: newlyAddedData,
    	})
    	.success(function(res) {
    		
            if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

    	})
    	.error(function(err) {
    		promptCache.errorPrompt('新增失败！');
            return false;
    	});
    };

    // details
    $scope.details = function(i) {

    	$http({
    		url 	: $scope.zsUrl+'digitalhome-boss/open/scopes/getScopesInfo/'+$scope.dataID[i],
    		method 	: 'GET',

    	}).success(function(res) {

    		if( '0000' != res.result_code ) {
    			return false;
    		}

    		var obj = res.result;

    		$scope.details_scopesCode 	= obj.scopesCode;
    		$scope.details_scopeName 	= obj.scopeName;
    		$scope.details_scopeUri 	= obj.scopeUri;
    		$scope.details_scopeType 	= obj.scopeType;	// 资源类型
    		$scope.details_serviceTypeName 	= obj.serviceTypeName;	// 服务类型 name
    		$scope.details_description 	= obj.description;

    	}).error(function(err) {
    		console.log(err);
    	});
    };

    // edit: show data
    $scope.edit = function(i, edit_serviceType) {

    	getServiceTypeData('edit');

    	$http({
    		url 	: $scope.zsUrl+'digitalhome-boss/open/scopes/getScopesInfo/'+$scope.dataID[i],
    		method 	: 'GET',

    	}).success(function(res) {

    		if( '0000' != res.result_code ) {
    			return false;
    		}

    		$scope.index = i;

    		var obj = res.result;

    		$scope.edit_scopesCode 			= obj.scopesCode;
    		$scope.edit_scopeName 			= obj.scopeName;
    		$scope.edit_scopeUri 			= obj.scopeUri;
    		$scope.edit_scopeType 			= obj.scopeType;		// 资源类型
    		$scope.edit_serviceType         = obj.serviceType;		// 服务类型
    		$scope.edit_serviceTypeName 	= obj.serviceTypeName;	// 服务类型 name
    		$scope.edit_description 		= obj.description;

    		// 资源类型
    		$scope.editScopeTypeSelect = 
    		[
    			{ 'id' : 0, 'name': '系统资源' },
    			{ 'id' : 1, 'name': '用户资源' },
    		];

    	}).error(function(err) {
    		console.log(err);
    	});
    };

    // edit: submit data
    $scope.confirmEdit = function() {

    	var editData = {
    		scopesId		: $scope.dataID[$scope.index],
    		scopesCode 		: $scope.edit_scopesCode,
    		scopeName 		: $scope.edit_scopeName,
    		scopeUri 		: $scope.edit_scopeUri,
    		scopeType 		: $scope.edit_scopeType,
    		serviceType 	: $scope.edit_serviceType,
    		serviceTypeName : $scope.edit_serviceTypeName,
    		description 	: $scope.edit_description,
    	};

    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/open/scopes/modify',
    		method	: 'POST',
    		data 	: editData,

    	}).success(function(res) {
    		
            if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();  
    		
    	}).error(function(err) {
    		promptCache.errorPrompt('修改失败！');
            return false;
    	});
    };

    // delete
    $scope.getDataID = function() {

    	// data length
    	var len = $scope.dataID.length;
    	// param id
    	var whichId = 'scopesId';
    	// param
    	var whichUrl = 'open/scopes/delete';

    	// multiple selections 
        $scope.selectAll= function (bool, obj) {
            selectCache.selectAll(bool, $scope.dataID);
        };

        //  Single or multiple selections 
        $scope.select= function (selectObj, bool) {   
         	var selectId = selectObj.scopesId;        
            selectCache.select(selectId, bool, len);    
        };

        // check
        $scope.check= function () {
            selectCache.check();
        }; 

        // delete
        $scope.delete= function () { 
            selectCache.delete(whichId, whichUrl);
            reloadTable();
        };
    };

    // 从后台获取服务类型下拉框的数据
    function getServiceTypeData(whichFn) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/sysConfig/getSysConfig/serviceType',
    		method	: 'GET',

    	}).success(function(res) {
			
			// newly added
    		$scope.newlyAddedServiceTypeSelect = [];
    		// edit
    		$scope.editServiceTypeSelect = [];

    		// show select data
    		angular.forEach(res.result, function(obj, i) {
    			if( 'newlyAdded' == whichFn ) {
    				$scope.newlyAddedServiceTypeSelect[i] = { 'id' : obj.sysValue, 'name': obj.displayName };
    			
    			}else if( 'edit' == whichFn ) {
    				$scope.editServiceTypeSelect[i] = { 'id' : obj.sysValue, 'name': obj.displayName };
    			}
    		});

    		// get display name
    		$scope.getDisplayName = function() {

    			angular.forEach(res.result, function(obj, i) {

    				if( obj.sysValue == $scope.newlyAdded_serviceType ) {
    				
    					$scope.newlyAdded_serviceTypeName = obj.displayName;
    					return false;
    				
    				}else if( obj.sysValue == $scope.edit_serviceType ) {

    					$scope.edit_serviceTypeName = obj.displayName;
    					return false;

    				}
    			});
    		}; 

    	}).error(function(err) {
    		// console.log(err);
    	});
    };

    // reload table
    function reloadTable() { 
        $timeout(function() {
            $scope.paginationConf.onChange();
            $scope.master = false;
        },1500);
    };
}])
.directive('scopeNewlyadded', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/scope-authorization/newlyAdded.html',
		replace			: true,
	}
})
.directive('scopeDetails', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/scope-authorization/details.html',
		replace			: true,
	}
})
.directive('scopeEdit', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/scope-authorization/edit.html',
		replace			: true,
	}
})
.directive('scopeDelete', function() {
    return {
        restrict        : 'E',
        templateUrl     : 'system/openPlatform/scope-authorization/delete.html',
        replace         : true,
    }
})
.directive('scopeTable', function() {
    return {
        restrict        : 'E',
        templateUrl     : 'system/openPlatform/scope-authorization/table.html',
        replace         : true,
    }
});
});